package com.gzhryc.shared_device.oem.h5.mng.controllers.market.cloud;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.data.dto.SelectResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.cloud.goods.dao.db.CloudGoods;
import com.gzhryc.net_marketing.cloud.goods.dao.db.CloudGoodsSpace;
import com.gzhryc.net_marketing.cloud.goods.dao.db.GoodsType;
import com.gzhryc.net_marketing.cloud.goods.services.CloudGoodsService;
import com.gzhryc.net_marketing.cloud.goods.services.CloudGoodsSpaceService;
import com.gzhryc.net_marketing.cloud.goods.services.GoodsTypeService;
import com.gzhryc.net_marketing.cloud.goods.services.dto.CloudGoodsSearch;
import com.gzhryc.net_marketing.core.enums.EGoodsClassify;
import com.gzhryc.net_marketing.core.enums.EGoodsState;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CloudGoodsController {

    @Route(value = { "/market/cloud/goods/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", EGoodsState.values());
        request.setAttribute("classifyList", EGoodsClassify.values());
        return "/market/cloud/goods/list.html";
    }

    @Route(value = "/market/cloud/goods/list.data")
    public ListResult<CloudGoods> listData(HttpServletRequest request) {
        ListResult<CloudGoods> result = new ListResult<CloudGoods>(DtoConstants.NULL_DATA, "暂无数据");
        CloudGoodsSearch search = WebUtils.getParameter(request, CloudGoodsSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        Double maxPayMoneyStr = WebUtils.getDouble(request,"maxPayMoneyStr");
        if(maxPayMoneyStr != null){
            search.setMaxPayMoney(NumberTools.changeMoney(maxPayMoneyStr));
        }
        Double minPayMoneyStr = WebUtils.getDouble(request,"minPayMoneyStr");
        if(minPayMoneyStr != null){
            search.setMinPayMoney(NumberTools.changeMoney(minPayMoneyStr));
        }

        String types = WebUtils.getString(request,"types");
        if(StringTools.isNotBlank(types)) {
            String typeCodes = "";
            String[] ids = types.split(",");
            List<GoodsType> goodsTypeList = GoodsTypeService.self().findByIds(ids);
            if (goodsTypeList != null && goodsTypeList.size() > 0) {
                for (String id : ids) {
                    Long temp = Long.valueOf(id);
                    for (GoodsType goodsType : goodsTypeList) {
                        if (goodsType.getId().equals(temp)) {
                            typeCodes = typeCodes + "," + goodsType.getCode();
                            break;
                        }
                    }
                }
                if(typeCodes.length() > 0){
                    typeCodes = typeCodes.substring(1);
                }
                search.setTypeCodes(typeCodes);
            }
        }

        result.getBody().setTotal(CloudGoodsService.self().countBySearch(search));
        result.getBody().setData(CloudGoodsService.self().findBySearch(search, pageNo, pageLimit));

        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = { "/market/cloud/goods/info.html" })
    public String add(HttpServletRequest request) {
        String id = WebUtils.getString(request,"id");
        CloudGoods goods = CloudGoodsService.self().getById(id);
        if(goods != null) {
    		String resDomain = PropertyTools.get().getString("marketResDomain");
            String[] imgPaths = goods.getImgPaths();
            if (imgPaths != null && imgPaths.length > 0) {
                for (int i = 0; i < imgPaths.length; i++) {
                    if (StringUtils.isNotBlank(imgPaths[i]) && !imgPaths[i].startsWith("http")) {
                        imgPaths[i] = resDomain + imgPaths[i];
                    }
                }
            }
            goods.setImgPaths(imgPaths);
            String[] detailImgPaths = goods.getDetailImgPaths();
            if (detailImgPaths != null && detailImgPaths.length > 0) {
                for (int i = 0; i < detailImgPaths.length; i++) {
                    if (StringUtils.isNotBlank(detailImgPaths[i]) && !detailImgPaths[i].startsWith("http")) {
                        detailImgPaths[i] = resDomain + detailImgPaths[i];
                    }
                }
            }
            goods.setDetailImgPaths(detailImgPaths);
            WebUtils.setAttribute(request, goods);
            Long total = CloudGoodsSpaceService.self().countByGoodsId(id);
            if (total != null && total > 0) {
                request.setAttribute("hasGoodsSpace", true);
            }
        }
        return "/market/cloud/goods/info.html";
    }

    @Route(value = "/market/cloud/goods/space/list.data")
    public ListResult<CloudGoodsSpace> spaceListData(HttpServletRequest request) {
        ListResult<CloudGoodsSpace> result = new ListResult<CloudGoodsSpace>(DtoConstants.NULL_DATA, "暂无数据");
        String goodsId = WebUtils.getString(request,"goodsId");
        List<CloudGoodsSpace> data = CloudGoodsSpaceService.self().findByGoodsId(goodsId);
        if(data != null && data.size() > 0){
    		String resDomain = PropertyTools.get().getString("marketResDomain");
            for(CloudGoodsSpace goodsSpace : data){
                if (StringUtils.isNotBlank(goodsSpace.getThumbnailImg()) && !goodsSpace.getThumbnailImg().startsWith("http")) {
                    goodsSpace.setThumbnailImg(resDomain + goodsSpace.getThumbnailImg());
                }
            }
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route("/market/cloud/goods/select.data")
    public SelectResult selectData(HttpServletRequest request) {
        SelectResult result = new SelectResult(DtoConstants.SUCCESS);
        String term = request.getParameter("term");
        Integer page = WebUtils.getInteger(request, "page", 1);

        CloudGoodsSearch search = new CloudGoodsSearch();
        search.setName(term);

        Long total = CloudGoodsService.self().countBySearch(search);
        List<CloudGoods> goodsList = CloudGoodsService.self().findBySearch(search,page,WebConstants.PAGE_LIMIT);
        if (goodsList != null && goodsList.size() > 0) {
            for (CloudGoods goods : goodsList) {
                SelectResult.Item item = new SelectResult.Item();
                item.setId(goods.getId());
                item.setText(goods.getName() + "(单价："+goods.getPayMoneyStr()+")");
                result.getBody().getData().add(item);
            }
        }
        result.getBody().setPage(page);
        result.getBody().setLimit(WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(total);
        return result;
    }

    @Route(value = { "/market/cloud/goods/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除商品失败");
        String id = WebUtils.getString(request, "id");
        if (StringUtils.isNotBlank(id)) {
            try {
                if (CloudGoodsService.self().deleteById(id)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
}
